package Controller.Announcement;

import Models.dao.AnnouncementDao;
import Models.enity.Announcement;
import Models.enity.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/announcement")
public class AnnouncementController extends HttpServlet {
    private static String PublicPath = "/WEB-INF/views";
    private AnnouncementDao announcementDao = new AnnouncementDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Announcement> announcements = announcementDao.findRecentlyWeeks();
        int currentPage = 1;
        if (req.getParameter("page") != null){
            currentPage = Integer.parseInt(req.getParameter("page"));
        }
        Pagination pagination = new Pagination(5,currentPage);
        int allData = (int)announcementDao.findDataSize();
        pagination.setAllPages(allData);
        pagination.PageData = announcementDao.findPaginationData(pagination.skip,pagination.getPageSize());
        req.setAttribute("announcements",announcements);
        req.setAttribute("pagination",pagination);
        req.getRequestDispatcher(PublicPath + "/announcement/announcement.jsp").forward(req,resp);
    }
}
